package com.wkg.music_player.controller;
/*import com.wkg.music_player.mapper.LoveMusicMapper;
import com.wkg.music_player.model.Music;
import com.wkg.music_player.model.User;
import com.wkg.music_player.tools.Constant;
import com.wkg.music_player.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;*/

import com.wkg.music_player.mapper.LoveMusicMapper;
import com.wkg.music_player.model.Music;
import com.wkg.music_player.model.User;
import com.wkg.music_player.tools.Constant;
import com.wkg.music_player.tools.ResponseBodyMessage;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/lovemusic")
public class LoveMusicController {
    @Autowired
    private LoveMusicMapper loveMusicMapper;

    @RequestMapping("/likeMusic")
    public ResponseBodyMessage<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);

        //1.是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1, "请登录上传", false);
        }

        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId:" + userId);
        //查询是否被收藏
        Music loveMusic = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId, musicId);
        if (loveMusic != null) {
            System.out.println("该用户已经收藏这首音乐!");
            return new ResponseBodyMessage<>(-1, "该用户已经收藏这首音乐!", false);
        }
        //收藏音乐
        Boolean effect = loveMusicMapper.insertLoveMusic(userId, musicId);
        if (!effect) {
            return new ResponseBodyMessage<>(-1, "该用户收藏这首音乐失败!", false);
        }
        return new ResponseBodyMessage<>(0, "该用户收藏这首音乐成功!", true);
    }

    @RequestMapping("/findlovemusic")
    public ResponseBodyMessage<List<Music>> findlovemusic(@RequestParam(required = false) String musicName,
                                                          HttpServletRequest request) {

        //1.是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录");
            return new ResponseBodyMessage<>(-1, "请登录再查找!", null);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId:" + userId);

        List<Music> musicList = null;
        try {

            if (musicName == null) {
                musicList = loveMusicMapper.findLoveMusicByUserId(userId);
            } else {
                musicList = loveMusicMapper.findLoveMusicBykeyAndUID(musicName, userId);
            }
            return new ResponseBodyMessage<>(0, "收藏歌曲查找成功", musicList);
        } catch (Exception e) {
            System.out.println("收藏歌曲查询失败!");
            return new ResponseBodyMessage<>(-1, "收藏歌曲查询失败!", musicList);
        }
    }

    @RequestMapping("/deletelovemusic")
    public ResponseBodyMessage<Boolean> deletelovemusic(@RequestParam String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        //1.是否登录
        HttpSession httpSession = request.getSession(false);
        if (httpSession == null || httpSession.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            System.out.println("没有登录!");
            return new ResponseBodyMessage<>(-1, "请登录后移除!", false);
        }
        User user = (User) httpSession.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getId();
        System.out.println("userId:" + userId);

        int ret = loveMusicMapper.deleteLoveMusic(userId, musicId);
        if (ret != 1) {
            return new ResponseBodyMessage<>(-1, "音乐取消收藏失败!", false);
        }
        return new ResponseBodyMessage<>(0, "音乐取消收藏成功!", true);
    }

}
